﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum CameraState
{
    AdjustState,
    LowerFightState,//近距离
    HighterFightState,//远距离
}
public class AutoChessCamera : MonoBehaviour
{
    private static AutoChessCamera ins;
    public static AutoChessCamera Ins
    {
        get { return ins; }
    }

    public Camera acCamera;
    public Transform defaultTrans;
    public Transform targetTrans;
    public Transform fightTrans;

    public float startFov = 45; 
    public float FightFov = 45;
    public float SeeFightFov = 25;

    public float zoomInTime = 0.8f;
    public float zoomOutTime = 0.8f;

    private float _epolse = 0;
    private Vector3 _startPos, _endPos;
    private Quaternion _startRote, _endRote;
    private float _startFov, _endFov;
    private float _value = 0;
    private bool _transing = false;
    private float _zoomTime = 1;
    private CameraState cameraState = CameraState.AdjustState;
    void Awake()
    {
        ins = this;
        startFov = getFov(startFov);
        FightFov = getFov(FightFov);
        SeeFightFov = getFov(SeeFightFov);
    }
    public void SwitchCamera(bool active)
    {
        acCamera.gameObject.SetActive(active);
    }
    public float getFov(float fov)
    {
        float manualWidth = 1280;
        float manualHeight = 760;
        float aspect = (float)Screen.width / (float)Screen.height;
        float manualAspect = manualWidth / manualHeight;
        if (aspect >= manualAspect)
        {
            return fov;
        }
        else
        {
            float scale = manualAspect / aspect;
            return fov * scale;
        }
    }

    private void Update()
    {
        if (_transing)
        {
            _epolse += Time.deltaTime;

            _value = _epolse / _zoomTime;
            acCamera.transform.position = Vector3.Lerp(_startPos, _endPos, _value);
            acCamera.transform.rotation = Quaternion.Lerp(_startRote , _endRote , _value);
            acCamera.fieldOfView = Mathf.Lerp(_startFov, _endFov, _value);

            if (_epolse >= _zoomTime)
            {
                _transing = false;
            }
        }
    }

#if UNITY_EDITOR
    [Sirenix.OdinInspector.Button("拉近")]
#endif
    public void ZoomInCamera()
    {
        _epolse = 0;
        _zoomTime = zoomInTime;
        _startPos = defaultTrans.position;
        _endPos = targetTrans.position;
        _startRote = defaultTrans.rotation;
        _endRote = targetTrans.rotation;
        _startFov = startFov;
        _endFov = FightFov;
        _transing = true;
        cameraState = CameraState.HighterFightState;
    }
#if UNITY_EDITOR
    [Sirenix.OdinInspector.Button("离开")]
#endif
    public void ZoomOutCamera()
    {
        _epolse = 0;
        _zoomTime = zoomOutTime;
        _startPos = targetTrans.position;
        _endPos = defaultTrans.position ;
        _startRote = targetTrans.rotation;
        _endRote = defaultTrans.rotation ;
        _startFov = FightFov;
        _endFov = startFov;
        _transing = true;
        cameraState = CameraState.AdjustState;
    }
#if UNITY_EDITOR
    [Sirenix.OdinInspector.Button("拉近战斗")]
#endif
    public void ZoomInFightCamera()
    {
        _epolse = 0;
        _zoomTime = zoomOutTime;
        _startPos = targetTrans.position;
        _endPos =   fightTrans.position;

        _startRote = targetTrans.rotation;
        _endRote = fightTrans.rotation;

        _startFov = FightFov;
        _endFov = SeeFightFov;
        _transing = true;
        cameraState = CameraState.LowerFightState;
    }
#if UNITY_EDITOR
    [Sirenix.OdinInspector.Button("拉远战斗")]
#endif
    public void ZoomOutFightCamera()
    {
        _epolse = 0;
        _zoomTime = zoomOutTime;
        _startPos = fightTrans.position ;
        _endPos = targetTrans.position;

        _startRote = fightTrans.rotation;
        _endRote = targetTrans.rotation ;

        _startFov = SeeFightFov ;
        _endFov = FightFov;
        _transing = true;
        cameraState = CameraState.HighterFightState;
    }
    public void SwitchCamera(CameraState state)
    {
        _transing = false;
        if (state == CameraState.HighterFightState)
        {
            acCamera.transform.position = targetTrans.position;
            acCamera.transform.rotation = targetTrans.rotation;
            acCamera.fieldOfView = FightFov;
        }
        else if (state == CameraState.LowerFightState)
        {
            acCamera.transform.position = fightTrans.position;
            acCamera.transform.rotation = fightTrans.rotation;
            acCamera.fieldOfView = SeeFightFov;
        }
    }
    public void OnDestroy()
    {
        ins = null;
    }
}
